using System.Configuration;
using System.Configuration.Provider;
using System.Web.Configuration;


namespace Wax.Provider
{
    /// <summary>
    /// Allows access to the CmsProviders, either using the defualt provider <see cref="Provider">CmsManger.Provider</see>
    ///  or by explicitly specifying a provider using the <see cref="Providers" /> property
    /// </summary>
    public sealed class WaxDataManager
    {
        private static WaxDataProviderBase defaultProvider;
        private static WaxDataProviderCollection providerCollection = InitializeProviderCollection();

        private WaxDataManager(){}
        
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters", MessageId = "System.Configuration.Provider.ProviderException.#ctor(System.String)")]
        private static WaxDataProviderCollection InitializeProviderCollection()
        {

            try
            {
                //Get the feature's configuration info
                WaxDataProviderConfiguration qc =
                    (WaxDataProviderConfiguration)ConfigurationManager.GetSection("Wax.Data.Configuration");

                if (qc == null)
                    throw new ConfigurationErrorsException("No Wax.Data.Configuration found in your configuration file.");

                if (qc.Providers == null)
                    throw new ConfigurationErrorsException("No Wax.Data.Configuration found in your configuration file.");
           
                if (qc.DefaultProvider == null || qc.Providers.Count < 1)
                    throw new ProviderException("You must specify a valid default provider.");

                //Instantiate the providers
                providerCollection = new WaxDataProviderCollection();
                ProvidersHelper.InstantiateProviders(qc.Providers, providerCollection, typeof(WaxDataProviderBase));
                
                providerCollection.SetReadOnly();
                defaultProvider = providerCollection[qc.DefaultProvider];
                if (defaultProvider == null)
                {
                    throw new ConfigurationErrorsException(
                        "You must specify a default provider for the feature.",
                        qc.ElementInformation.Properties["defaultProvider"].Source,
                        qc.ElementInformation.Properties["defaultProvider"].LineNumber);
                }
            }
            catch
            {
                throw;
            }
            return providerCollection;
        }

        /// <summary>
        /// Gets the default provider.
        /// </summary>
        public static WaxDataProviderBase Provider
        {
            get
            {
                return defaultProvider;
            }
        }

        /// <summary>
        /// Gets the collection of providers.
        /// </summary>
        public static WaxDataProviderCollection Providers
        {
            get
            {
                return providerCollection;
            }
        }
    }
}
